/**
 * merge level=-1
 */

Ext.define('Jfok.login.LoginForm', {
	extend : 'Ext.form.Panel',
	alias : 'widget.loginform',
	frame : true,
	id : 'loginform',
	title : '系统登录',
	icon : 'images/login/loginin.png',
	tools : [{
				type : 'refresh',
				tooltip : '重置所有',
				handler : function(event, toolEl, panel) {
					this.up('form').getForm().reset();
					errormessage.setText('');
					this.up('form').down('textfield[name=loginname]')['focus']();
				}
			}, {
				type : 'help',
				tooltip : '忘记密码？',
				handler : function(event, toolEl, panel) {
					Ext.Ajax.request({
								url : 'login/forgetPassword.do',
								success : function(response) {
									var text = response.responseText;
									Ext.MessageBox.show({
												title : '消息',
												msg : text,
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.INFO
											});
								}
							});
				}
			}],
	bodyPadding : 10,
	width : 420,
	shadow : true,
	shadowOffset : 10,
	url : 'login/login.do',
	layout : 'anchor',
	defaultType : 'textfield',

	initComponent : function() {
		this.codehidden = !(sinfo.needIdentifingCode && (sinfo.alwaysNeedIdentifingCode || sinfo.isNeedIdentifyingCode));
		this.defaults = {
			msgTarget : 'side',
			labelWidth : 60,
			margin : '20 10 10 30'
		};
		this.items = [{
					fieldLabel : '用户名',
					name : 'loginname',
					maxLength : 20,
					enforceMaxLength : true,
					allowBlank : false,
					enableKeyEvents : true,
					listeners : {
						keydown : function(field, e, eOpts) {
							if (e.getKey() == Ext.EventObject.ENTER)
								field.up('form').down('[name=password]').focus();
						}
					}
				}, {
					fieldLabel : '密　码',
					name : 'password',
					inputType : 'password',
					maxLength : 10,
					enforceMaxLength : true,
					submitValue : false,
					allowBlank : false,
					enableKeyEvents : true,
					listeners : {
						keydown : function(field, e, eOpts) {
							if (e.getKey() == Ext.EventObject.ENTER)
								if (field.up('form').down('[id=verifyCodeContainer]').hidden)
									field.up('form').loginin();
								else
									field.up('form').down('textfield[name=identifyingCode]')
											.focus(true, true);
						}
					}
				}, {
					xtype : 'container',
					layout : {
						type : 'hbox',
						align : 'middle'
					},
					id : 'verifyCodeContainer',
					hidden : this.codehidden,
					items : [codeField = Ext.widget('textfield', {
										labelWidth : 60,
										name : 'identifyingCode',
										msgTarget : 'side',
										width : 120,
										fieldLabel : '验证码',
										enforceMaxLength : true,
										allowBlank : this.codehidden,
										maxLength : 4,
										enableKeyEvents : true,
										listeners : {
											keydown : function(field, e, eOpts) {
												if (e.getKey() == Ext.EventObject.ENTER)
													field.up('form').loginin();
											}
										}
									}), codeimage = Ext.widget('image', {
										id : 'codeimage',
										title : '看不清？点击换一张',
										margin : '0 0 0 10',
										height : 30,
										src : 'login/IdentifyingCodeServlet',
										listeners : {
											afterrender : function(image) {
												Ext.get('codeimage').on('click', function() {
													timeNow = new Date().getTime();
													codeimage.setSrc(image.src.split('?')[0] + '?d='
															+ timeNow);
													codeField.reset();
													codeField.focus();
												})
											}
										}
									})]
				}, {
					hideEmptyLabel : false,
					name : 'saveusername',
					boxLabel : '保存用户名',
					xtype : 'checkbox',
					margin : '30 10 0 30'
				}, {
					hideEmptyLabel : false,
					boxLabel : '二周内自动登录',
					hidden : sinfo.autoLoginInTwoWeeks != true,
					name : 'autologinintwoweeks',
					xtype : 'checkbox',
					margin : '0 10 10 30'
				}, {
					inputType : 'hidden',
					margin : '',
					name : 'base64password',
					value : ''
				}, errormessage = Ext.widget("label", {
							xtype : 'label',
							id : 'errorMessage',
							text : ''
						})]
		this.buttons = ['->', {
					text : '登 录',
					scale : 'large',
					width : 100,
					handler : function() {
						this.up('form').loginin();
					}
				}, '->'];
		this.callParent(arguments);
	},

	loginin : function() {
		var loginform = this;
		var form = this.getForm();
		if (form.isValid()) {
			var base64pass = (new Base64).encode(loginform
					.down('textfield[name=password]').getValue());
			loginform.down('[name=base64password]').setValue(base64pass);
			form.submit({
				success : function(form1, action) {
					if (loginform.down('checkbox[name=autologinintwoweeks]').getValue()) {
						loginform.down('checkbox[name=saveusername]').setValue(true)
						Cookies.set('autoLoginInTwoWeeks', 'true', 15);
						Cookies.set('password', base64pass);
					} else {
						Cookies.set('autoLoginInTwoWeeks', '', -1);
						Cookies.set('password', '', -1);
					}

					if (loginform.down('checkbox[name=saveusername]').getValue()) {
						Cookies.set('saveusername', 'true');
						Cookies.set('loginname', loginform
										.down('textfield[name=loginname]').getValue());
					} else {
						Cookies.set('saveusername', 'false');
						Cookies.set('loginname', '', -1);
					}
					location.href = "index.jsp";
				},
				failure : function(form1, action) {
					if (action.result == undefined) {
						alert('服务器超时或没有响应,请与服务单位进行联系！');
						return;
					}
					if (sinfo.needIdentifingCode) {
						loginform.down('[id=verifyCodeContainer]').setVisible(true);
						loginform.down('[name=identifyingCode]').allowBlank = false;
					}
					errormessage.setText(action.result.msg);
					if (action.result.msg.indexOf('验证码') > 0)
						loginform.down('textfield[name=identifyingCode]').focus(true, true);
					else
						loginform.down('textfield[name=password]').focus(true, true);
				}
			});
		}
	},

	listeners : {
		afterrender : function() {
			var loginname = Cookies.get('loginname', '');
			var loginField = this.down('textfield[name=loginname]');
			loginField.setValue(loginname);
			if (loginname)
				this.down('textfield[name=password]').focus();
			else
				loginField.focus();
			this.down('checkbox[name=saveusername]').setValue(Cookies.get(
							'saveusername', '') != 'false');
			this.down('checkbox[name=autologinintwoweeks]').setValue(Cookies.get(
							'autoLoginInTwoWeeks', '') == 'true');
			Ext.get("loading").remove();
		}
	}
});
